package com.huaxia.day13;

/**
 * 堆栈信息查看
 * jps 查看进程
 * jstack 进程号
 */
public class DeadLockTest {
    public static void main(String[] args) throws Exception{
        Object lockObj1 = new Object();
        Object lockObj2 = new Object();
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lockObj1) {
                    try {
                        System.out.println(Thread.currentThread().getName() + " get lockObj1");
                        Thread.sleep(500);
                        System.out.println(Thread.currentThread().getName() + "sleep 500ms!");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + " waiting lockObj2");
                    synchronized (lockObj2) {
                        System.out.println(Thread.currentThread().getName() + " get lockObj2");
                    }
                }
            }
        }, "thread-1");
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lockObj2) {
                    try {
                        System.out.println(Thread.currentThread().getName() + " get lockObj2");
                        Thread.sleep(500);
                        System.out.println(Thread.currentThread().getName() + "sleep 500ms!");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + " waiting lockObj1");
                    synchronized (lockObj1) {
                        System.out.println(Thread.currentThread().getName() + " get lockObj1");
                    }
                }
            }
        }, "thread-2");

        thread1.start();
        thread2.start();
        Thread.sleep(100);
         System.out.println(thread1.getState());
          System.out.println("----------");
        System.out.println(thread2.getState());
    }


}
